package com.ala4.oxcafe.boot.authentication;

import com.ala4.oxcafe.boot.manager.UserTokenManager;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.oauth2.server.resource.authentication.BearerTokenAuthenticationToken;

/**
 * 鉴权token的provider
 *
 * @author PING
 * @version 1.0
 * @date 2025/3/4 18:18
 */
public class CustomAuthenticationProvider implements AuthenticationProvider {

    private final UserTokenManager userTokenManager;

    public CustomAuthenticationProvider(UserTokenManager userTokenManager) {
        this.userTokenManager = userTokenManager;
    }

    @Override
    public Authentication authenticate(Authentication authentication) throws AuthenticationException {
        BearerTokenAuthenticationToken bearer = (BearerTokenAuthenticationToken) authentication;
        // 直接从token转换成用户信息
        return userTokenManager.checkLoginUserTokenA(bearer.getToken());
    }

    @Override
    public boolean supports(Class<?> authentication) {
        return BearerTokenAuthenticationToken.class.isAssignableFrom(authentication);
    }
}
